package com.hrt.manage.product.controllor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.GiftTypeDir;
import com.hrt.manage.product.service.GiftInforService;
import com.hrt.manage.product.service.GiftTypeDirService;
import com.hrt.manage.product.service.GiftTypeService;
import com.hrt.vo.GiftTypeDirVO;
import com.hrt.vo.GiftTypeVO;

@Controller
@RequestMapping("manage/giftTypeDir")
public class GiftTypeDirControllor extends BaseController<GiftTypeDir> {

	/**礼品分类目录明细*/
	@Autowired
	private GiftTypeDirService giftTypeDirService;
	
	/**礼品分类*/
	@Autowired
	private GiftTypeService giftTypeService;
	
	/**礼品基本信息*/
	@Autowired
	private GiftInforService giftInforService;
	
	/**ajax 搜索框*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryGiftInfors")
	@ResponseBody
	public Map queryGiftInfors(
			@RequestParam(value = "condition") String itemCondition,
			HttpServletRequest request, HttpServletResponse response){
		String condtionStr = null;
		List giList = new ArrayList();
		Map result = new HashMap();	
		try {
			condtionStr = new String(itemCondition.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(condtionStr != null && !"".equals(condtionStr)){
			//获取礼品的模糊查询的内容
			giList = this.giftInforService.queryGiftInfors(condtionStr);
		}
		result.put("result", giList);
		return result;
	}
	
	/**
	 * 加载一个礼品实体
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/loadGiftInfor")
	@ResponseBody
	public Map loadGiftInfor(
			@RequestParam(value = "id", required = true) String giftInforId,
			HttpServletRequest request, HttpServletResponse response) {
		Map map = new HashMap();
		if (giftInforId != null && !"".equals(giftInforId)) {
			//获取礼品的实体
			GiftInfor giftInfor = this.giftInforService.getById(new Long(giftInforId));
			GiftTypeDirVO dirVO = new GiftTypeDirVO();
			dirVO.setGiftId(giftInfor.getId());
			dirVO.setGiftName(giftInfor.getGiftName());
			if(giftInfor != null){
				map = transBean2Map(dirVO);
			}
		}
		return map;
	}
	
	/**
	 * 删除产品组合详情
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/delItemById")
	@ResponseBody
	public boolean delItemById(
			@RequestParam(value = "id", required = true) Long itemId,
			HttpServletRequest request, HttpServletResponse response) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (itemId != null && !"".equals(itemId)) {
			if (itemId != null) {
				return giftTypeDirService.delItemById(itemId);
			}
		}
		return false;
	}
	
	/**
	 * 添加修改产品组合(注入上一级giftTypeVO)
	 * */
	@RequestMapping(value = "/saveGTypeDir", method = RequestMethod.POST)
	public void saveGTypeDir(
			@ModelAttribute("giftTypeDir") GiftTypeVO giftTypeVO,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		List<GiftTypeDirVO> giftTypeDirVO = new ArrayList<GiftTypeDirVO>();
		if(giftTypeVO.getGiftTypeDirVO() !=null){
			for(GiftTypeDirVO dirVO : giftTypeVO.getGiftTypeDirVO()){
				if(dirVO.getId() == null && dirVO.getGiftId() != null){
					dirVO.setIsdelete(0);
					giftTypeDirVO.add(dirVO);
				}
				if(dirVO.getId() != null && dirVO.getGiftId() != null){
					giftTypeDirVO.add(dirVO);
				}
			}
			this.giftTypeDirService.updateGiftTypeDirs(giftTypeDirVO);
		}
		response.sendRedirect("/HRTWeb/manage/giftType/list.do");
	}
	
	/**根据礼品规格的ID找到产品组合的列表*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryDir")
	@ResponseBody
	public Map queryDir(
			@RequestParam(value = "id", required = true) Long typeId,
			HttpServletRequest request, HttpServletResponse response) {
		Map resultMap = new HashMap();
		List<GiftTypeDir> resultList = this.giftTypeDirService.getGiftTypeDirByGdId(typeId);
		resultMap.put("resultList", resultList);
		resultMap.put("gtdId", typeId);
		return resultMap;
	}

}
